﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;

using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Core;

namespace Ksamuelsen.Office.Outlook {
    public class OutlookItemContextMenuHandler : IDisposable {
        private Application _app;


        public OutlookItemContextMenuHandler(Application app) {
            _app = app;
            _app.ItemContextMenuDisplay += new ApplicationEvents_11_ItemContextMenuDisplayEventHandler(OnItemContextMenuDisplay);
            _app.ContextMenuClose       += new ApplicationEvents_11_ContextMenuCloseEventHandler(OnContextMenuClose);
        }

        public void Dispose() {
            _app.ItemContextMenuDisplay -= new ApplicationEvents_11_ItemContextMenuDisplayEventHandler(OnItemContextMenuDisplay);
            _app.ContextMenuClose       -= new ApplicationEvents_11_ContextMenuCloseEventHandler(OnContextMenuClose);
            _app = null;
        }

        protected virtual void OnItemContextMenuDisplay(CommandBar cmd, Selection sel) {
        }

        protected virtual void OnContextMenuClose(OlContextMenu ctxMenu) {

        }

        public static CommandBarPopup AddCommandBarPopup(CommandBar parent, string caption, string tag) {
            CommandBarPopup menu = (CommandBarPopup)parent.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            menu.Caption = caption;
            menu.Visible = true;
            menu.Tag = tag;

            return menu;
        }

        public static CommandBarButton AddCommandBarButton(CommandBarPopup parent, string caption, string tag) {
            CommandBarButton btn = (CommandBarButton)parent.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            btn.Caption = caption;
            btn.Visible = true;
            btn.Tag = tag;

            return btn;
        }



    }
}
